﻿// ==================================================
// Copyright © 2016 杭州伯庸科技 版权所有
//
// 文 件 名：SaveAsWorkspace
// 功能描述：
//
// 创建标识：Yu.Liang(俞 亮) 2018/3/6 10:32:02
//
// 修改标识：
// 修改描述：
// ==================================================

using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using Shine.Framework;
using SuperMap.Data;
using System;
using Shine.Framework;
using System.Diagnostics;
using System.Windows.Forms;

namespace Shine.File
{
    internal class SaveAsWorkspace:BaseCommand
    {
        IApplication m_Application = null;

        public SaveAsWorkspace()
        {
            base.m_caption = "另存为…";
            base.m_name = "SaveAsWorkspace";
            base.m_toolTip = "另存工作空间";
            base.m_message = "另存工作空间";
            base.m_category = "file";
        }

        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
        }

        public override int Bitmap
        {
            get
            {
                string imgFile = Application.StartupPath + "\\Images\\" + GetType().Name + ".png";

                if (System.IO.File.Exists(imgFile) == false) return 0;

                return new Bitmap(Image.FromFile(imgFile)).GetHbitmap().ToInt32();
            }
        }
        public override bool Enabled
        {
            get { return (m_Application as IFramework).WorkspaceControl.WorkspaceTree.Workspace != null; }
        }

        public override void OnClick()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "工作空间文件(*.sxwu,*.smwu) | *.sxwu;*.smwu";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(dlg.FileName);

                if (System.IO.Path.GetExtension(dlg.FileName).ToLower() == ".sxwu")
                {
                    conInfo.Type = WorkspaceType.SXWU;
                }
                else
                {
                    conInfo.Type = WorkspaceType.SMWU;
                }

                try
                {
                    Boolean isSucceed = (m_Application as IFramework).WorkspaceControl.WorkspaceTree.Workspace.SaveAs(conInfo);

                    if (isSucceed)
                    {
                        MessageBox.Show("工作空间保存成功!");
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
            }
        }

    }
}
